<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="省" prop="cityId">
        <el-select v-model="queryParams.cityId" placeholder="请输入省" @change="changeCityId" clearable>
          <el-option

            v-for="item in cityOpt"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="市" prop="provinceId">
        <el-select v-model="queryParams.provinceId" placeholder="请输入市" @change="changeProvinceId" clearable>
          <el-option
            v-for="item in provinceOpt"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="区" prop="countId">
        <el-select v-model="queryParams.countId" placeholder="请输入区" clearable>
          <el-option
            v-for="item in countOpt"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="showingsNames">
        <el-input
          v-model="queryParams.showingsNames"
          placeholder="请输入楼盘关键字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="info" @click="dialogFormVisible=true">预约看房</el-button>
      </el-form-item>
    </el-form>




    <el-table v-loading="loading" :data="reservationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="预约人" align="center" prop="showingsName" />
      <el-table-column label="预约房源" align="center" prop="roomname" />
      <el-table-column label="楼盘名称" align="center" prop="premissname" />
      <el-table-column label="联系人电话" align="center" prop="showingsMobile" />
      <el-table-column label="预约时间" align="center" prop="reserveTime" />
      <el-table-column label="预约状态" align="center" prop="status" />

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="预约看房" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="联系电话" :label-width="formLabelWidth">
          <el-input v-model="form.showingsMobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="顾客ID" :label-width="formLabelWidth">
          <el-input v-model="form.customerId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预约房间信息" :label-width="formLabelWidth">
          <el-select v-model="form.roomId">
            <span v-for="item in RoomOpts">
              <el-option :value="item.value" :label="item.label"></el-option>
            </span>

          </el-select>
        </el-form-item>
        <el-form-item label="预约时间" :label-width="formLabelWidth">
          <div class="block">
            <span class="demonstration">默认</span>
            <el-date-picker
              v-model="form.reserveTime"
              type="datetime"
              placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addReservations">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addReservations,getRoomOpts,listReservation,changecountId, getReservation,changeCity,changeProvince, delReservation, addReservation, updateReservation } from "@/api/termination/reservation";

export default {
  name: "Reservation",
  components: {
  },
  data() {
    return {
      RoomOpts:[],
      formLabelWidth:"120px",
      dialogFormVisible:false,
      countOpt:[],
      provinceOpt:[],
      cityOpt:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      countId:"",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      provinceId:"",
      // 预约房源表格数据
      reservationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      CityId:"",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomId: null,
        customerId: null,
        showingsName: null,
        showingsSex: null,
        showingsMobile: null,
        userId: null,
        reserveTime: null,
        status: null,
        cityId: null,
        provinceId: null,
        countId: null,
        showingsNames: null,
        reserve5: null,
      },
      // 表单参数
      form: {
        showingsMobile:"",
        customerId:"",
        roomId:"",
        reserveTime:""
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.changeCity();
    this.getRoomOpts();
  },
  methods: {
    addReservations(){
      addReservations(this.form).then(resp=>{
        if(resp>0){
          alert("预约成功")
          this.dialogFormVisible = false
        }else{
          alert("预约失败")
        }

      })
    },
    getRoomOpts(){
      getRoomOpts().then(resp=>{
        console.log(resp)
        this.RoomOpts=resp.data
      })
    },
    changeCity(){
      changeCity().then(resp=>{
        this.cityOpt = resp.rows
      })
    },
    changeCityId(){
      this.provinceOpt = null
      this.provinceId = this.queryParams.cityId
      changeProvince(this.provinceId).then(resp=>{
        this.provinceOpt = resp.rows
      })
    },
    changeProvinceId(){
      this.countOpt = null
      this.countId = this.queryParams.provinceId
      changecountId(this.countId).then(resp=>{
        this.countOpt = resp.rows
      })
    },
    /** 查询预约房源列表 */
    getList() {
      this.loading = true;
      listReservation(this.queryParams).then(response => {
        this.reservationList = response.rows;
        response.rows.forEach(i=>{
          if(i.status==0){
            i.status="待看房"
          }else{
            i.status="已看房"
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        roomId: null,
        customerId: null,
        showingsName: null,
        showingsSex: null,
        showingsMobile: null,
        userId: null,
        reserveTime: null,
        status: null,
        cityId: null,
        provinceId: null,
        countId: null,
        ShowingsNames: null,
        reserve5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约房源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReservation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约房源";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReservation(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReservation(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除预约房源编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReservation(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/reservation/export', {
        ...this.queryParams
      }, `system_reservation.xlsx`)
    }
  }
};
</script>
